//1.引入样式表
//公用样式 
require('../../assets/css/index.less');
require('../../assets/fonts/iconfont.css');
// weui
require("../../lib/weui/weui.css")
let weui = require("../../lib/weui/weui.js")
// 当前页面样式
require('./addcircle.less')

//2.引入工具函数 utils
const dom = require('../../utils/dom');
const https = require('../../utils/https');
const local = require('../../utils/local');
dom.ready(function () {
    dom.addEvent("#backOut", function () {
        history.go(-1)
    })
    let proValue = ''
    dom.addEvent("#proBtn", function () {
        https.get("/api/shared/province", function (res) {
            let { errno, data } = res
            console.log(res);
            if (errno == 0) {
                weui.picker(
                    data.map((v) => {
                        return {
                            label: v.name,
                            value: v.id
                        }
                    }), {
                    onConfirm: function (result) {
                        console.log(result);
                        dom.getEle("#proText").textContent = result[0].label
                        proValue = result[0].value
                        cityValue = ''
                        dom.getEle("#cityText").textContent = ''
                    },//点击确认
                    title: '省份',
                });
            }
        })
    })
    let cityValue = ''
    dom.addEvent("#cityBtn", function () {
        if (proValue) {
            https.get(`/api/shared/city?provinceId=${proValue}`, function (res) {
                let { errno, data } = res
                console.log(res);
                if (errno == 0) {
                    weui.picker(
                        data.map((v) => {
                            return {
                                label: v.name,
                                value: v.id
                            }
                        }), {
                        onConfirm: function (result) {
                            console.log(result);
                            dom.getEle("#cityText").textContent = result[0].label
                            cityValue = result[0].value
                        },//点击确认
                        title: '城市',
                    });
                }
            })
        }
    })

    https.get("/api/shared/province", res => {
        let province = res.data.filter(v => {
            return v.id == local.get("userData").provinceId
        })
        dom.getEle("#proText").textContent = province[0].name
        proValue = province[0].id
        https.get(`/api/shared/city?provinceId=${province[0].id}`, res => {
            let city = res.data.filter(v => {
                return v.id == local.get("userData").cityId
            })
            dom.getEle("#cityText").textContent = city[0].name
            cityValue = city[0].id
        })
    })
    dom.addEvent("#uploaderInput", function () {
        dom.file("/api/circles/uploadImg", '#uploaderInput', function (res) {
            let { errno, data } = res
            if (errno == 0) {
                // dom.getEle("#imgBtn").src = 'http://fitness.h5.itsource.cn:3701' + data.url
                imgUrl = data.url
                console.log(imgUrl);
            }
        })
    }, "change")
    let imgUrls=[];
    dom.addEvent("#uploaderInput", function (e) {
        dom.file("/api/circles/uploadImg", '#uploaderInput', function (res) {
            let { errno, data } = res
            if (errno == 0) {
                console.log(res);
                uploaderFiles.innerHTML+=`<li class="weui-uploader__file" role="img" aria-label="图片标题" title="轻点两下查看大图" tabindex="0" style="background-image: url(http://fitness.h5.itsource.cn:3701${data.url});"></li>`
                imgUrls.push(data.url)
            }
        })
    }, "change")
    dom.addEvent("#addcir", function () {
        if(intro.value){
            https.post("/api/circles/opinion", {
                content: dom.getEle("#intro").value,
                releaseProvinceId: proValue,
                releaseCityId: cityValue,
                imgUrls: imgUrls,
            }, res => {
                alert("上传成功")
            })
        }
    })
})
